package com.slipper.core.similarity;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.corpus.io.ByteArray;
import com.hankcs.hanlp.dictionary.stopword.StopWordDictionary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class HanLPStopWord {
    private static final Logger logger = LoggerFactory.getLogger(HanLPStopWord.class);
    static StopWordDictionary dictionary;

    static {
        ByteArray byteArray = ByteArray.createByteArray(HanLP.Config.CoreStopWordDictionaryPath + ".bin");
        if (byteArray != null) {
            dictionary = new StopWordDictionary();
            dictionary.load(byteArray);
        } else {
            logger.warn("加载HanLP停用词失败！");
        }
    }

    public static boolean contains(String stopWord) {
        return dictionary.contains(stopWord);
    }

    public static void add(String ...stopWords) {
        Collections.addAll(dictionary, stopWords);
    }

    public static void remove(String ...stopWords) {
        for (String stopWord : stopWords) {
            dictionary.remove(stopWord);
        }
    }

    public static List<String> clean(List<String> list) {
        if (CollectionUtils.isEmpty(list)) return Collections.emptyList();
        List<String> resultList = new ArrayList<>();
        for (String word : list) {
            if (!contains(word)) resultList.add(word);
        }
        return resultList;
    }
}
